﻿using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class TinMoiVn
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

       

            string urlGoc = "http://www.tinmoi.vn/";


            //  ListCate.AddRange(load.LoadCatesNoSub("https://vn.nang.yahoo.com/thoitrang/", "//ul//li[@id='yui_3_9_1_1_1398516153089_123']", urlGoc, manuId, "//a"));

            //   ListCate.AddRange(load.LoadCatesNoSub("https://vn.nang.yahoo.com", "//ul//li[@id='yui_3_9_1_1_1398516153089_92']", urlGoc, manuId, "//a"));




            ListCate = load.LoadCatesNoSub("http://www.tinmoi.vn/C/Giai-tri", "//ul[@class='menu-sub-nav']//li", "", manuId, "//a");



            int dem = 0;



            for (int i = 0; i < ListCate.Count(); i++)
            {
             

                

                 

                  //  p.Url = firstPage.Url + "/page/" + trang;
                  
                 
                    var ListDetail = load.LoadUpdateNewHomeNews(ListCate[i], "//ul[@class='list-post']//li", "//a", "//p", "//a", "");

                    //  Boolean flag = true;

              

                    // phai chay dong for nguoc, de xet phan tu dau tien

                    for (int k = 0; k < ListDetail.Count(); k++)
                    {
                        var item = load.LoadNews(ListDetail[k], "//div[@id='tm-content']//p", "", manuId, 3, "//div[@id='tm-content']//img", "",false);
                        if (item != null)
                        {
                          
                                dem++;

                                load.AddItemToListView(item);

                                pdb.SaveImgNormal(folder, item);

                            
                          

                        }
                     




                    }

                  

                
                //if (dem == 30)
                //    break;

            }
       //     System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
